package com.leetcode.week;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author angzhijin
 * @Description
 * @create 2022-09-04 1:49 PM
 */
public class Week309 {


    public static void main(String[] args) {
        Solution solution = new Solution();
//        solution.checkDistances("abaccb", new int[]{1,3,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0});
//        solution.numberOfWays(1,2,3);

        solution.longestNiceSubarray(new int[]{1,3,8,48,10});

    }

    static class Solution {
        /**
         * Week1
         */
        public boolean checkDistances(String s, int[] distance) {
            for (int i = 0; i < 26; i++) {
                char ch = (char)('a'+ i);
                int start = s.indexOf(ch);
                if(start!=-1){
                    int end = s.indexOf(ch, start+1);
                    if(distance[i]!=end-start-1)
                        return false;
                }
            }
            return true;
        }

        /**
         * Week2
         */
        public int numberOfWays(int startPos, int endPos, int k) {
            int MOD = (int)1e9 +7;
            int len = Math.abs(endPos - startPos);
            if(k<len || (k -Math.abs(len)) %2!=0)
                return 0;
            int[][] dp = new int[k+1][k+1];

            for (int i = 0; i <= k; i++) {
                dp[i][0] = 1;
                for (int j = 1; j <= i; j++) {
                    dp[i][j]=(dp[i-1][j] + dp[i-1][j-1]) % MOD;
                }
            }
            return dp[k][(k+len)/2];
        }

        /**
         * Week3
         */
        public int longestNiceSubarray(int[] nums) {
            int len = nums.length;
            if(len <1) return 1;

            int L =0, R=1;
            int max = 1;
            int tmp = nums[L];
            while(R<len){
                if(((tmp&nums[R]) ==0)){
                    tmp ^= nums[R]; // 保存已有的1位
                    max = Math.max(R-L+1, max);
                }else{
                    L++;
                    R=L;
                    tmp = nums[L];
                }
                R++;
            }
            return max;
        }
    }
}
